/*
    选择器 也是基于对象创建
*/

#include "selector.h"
#include "widget/widget.h"
#include "render.h"
#include <stdio.h>

typedef struct
{
    list_node_t *root;  // 根节点
    widget_t *sel_node; // 选中的对象
    widget_t *selector; // 选择器自身
} selector_t;

selector_t selector;

void selector_init(widget_t* root)
{
    selector.selector = widget_create();
    selector.root = root;
    selector.sel_node = selector.root;
}

// 获取选中节点
widget_t *selector_get_sel_node(void)
{
    return selector.sel_node;
}

keypad_event_e keypad_read(void)
{
    return keypad_event_none;
}

/// @brief 选择器运行
/// @param
void selector_run(void)
{
    keypad_event_e keypad_event = keypad_event_none;

    if (selector.sel_node->state == widget_state_none)
    {
        // 外部ui界面设置
        switch (keypad_event)
        {
        case keypad_event_up:
            selector.sel_node = list_entry(selector.sel_node->node.prev, widget_t, node);
            break;
        case keypad_event_down:
            selector.sel_node = list_entry(selector.sel_node->node.next, widget_t, node);
            break;
        case keypad_event_left:
            /* code */
            break;
        case keypad_event_right:
            /* code */
            break;
        case keypad_event_enter:
            selector.sel_node->state = (selector.sel_node->state == widget_state_selected) ? widget_state_none : widget_state_selected;
            break;
        case keypad_event_none:
            /* code */
            break;
        default:
            break;
        }
    }
    else
    {
        // 按键直通对象
        if(selector.sel_node->event_cb)
        {
            printf("node:%p read keypad value\r\n",selector.sel_node);
            selector.sel_node->event_cb(selector.sel_node, widget_event_key, keypad_event, selector.sel_node->user_data);
        }
    }
}